<?php

class SHIPPING_EXPORTAFACIL extends ISC_SHIPPING
{

function SHIPPING_EXPORTAFACIL(){
			parent::__construct();
			$this->_name = "EXPORTA FACIL";
			$this->_image = "image.jpg";
			$this->_description = "Modulo de Envio Exporta Facil Correios";
			$this->_help = "Modulo de Envio Exporta Facil Correios";
			$this->_enabled = $this->CheckEnabled();
			$this->_countries = array("all");
			$this->_showtestlink = false;
}

public function SetCustomVars()
{
	$this->_variables['displayname'] = array(
		'name'			=> 'Frete Internacional',
		'type'		=> 'textbox',
		'help'		=> 'Modulo de Envio Exporta Facil Correios',
		'default'	=> $this->GetName(),
		'savedvalue'	=> array(),
		'required'	=> true);
	$this->_variables['uforigem'] = array(
		'name'		=> 'UF de Origem',
		'type'		=> 'textbox',
		'default'	=> 'SP',
		'help'		=> 'UF de Origem dos produtos a serem exportados.',
		'required'	=> true);
	$this->_variables['tipoo'] = array("name" => 'Origem Tipo',
		   "type" => "dropdown",
		   "help" => '',
		   "default" => "I",
		   "required" => true,
		   "options" => array(
						'Capital' => "C",
						'Interior' => "I",
						),
			"multiselect" => false
		);

}

public function soNumero($str) {
    return preg_replace("/[^0-9]/", "", $str);
}

public function GetQuote()
{

$d = $this->Getcombinedshipdimensions();

$com = number_format($d['length'], 2);
$com = str_replace('.',',',$com);

$com1 = number_format($d['height'], 2);
$alt = str_replace('.',',',$com1);

$com2 = number_format($d['width'], 2);
$lar = str_replace('.',',',$com2);

$uf = $this->GetValue("uforigem");
$tipoo = $this->GetValue("tipoo");
$pais = $this->_destination_country['country_iso'];
$peso = number_format(max(ConvertWeight($this->_weight, 'gms'), 0.1), 0, '.', '');

$url = "http://www.correios.com.br/exportafacil/bb/Consulta.cfm?tipoConsulta=Geral&RESET=false&LOCALIDADE=".$tipoo."&LARGURA=".number_format($lar,0,'','')."&PROFUNDIDADE=".number_format($com,0,'','')."&PESO=".$peso."&ESPECIF=110&UFORIGEM=".$uf."&ALTURA=".number_format($alt,0,'','')."&PAIS=".$pais."";

//echo $url;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 2);
$html1 = curl_exec ($ch);
curl_close ($ch);

$html = explode('<preco_postal>', $html1);
$html2 = explode('</preco_postal>', $html[1]);
$this->_shippingcost = $html2[0];

$pra = explode('<prazo_entrega>', $html1);
$prazo = explode('</prazo_entrega>', $pra[1]);


if($this->_shippingcost > 0) {
$dias = $this->soNumero($prazo[0]);
$cota = new ISC_SHIPPING_QUOTE($this->GetId(), $this->GetValue("displayname"), $this->_shippingcost, '', $dias[1]);
return $cota;
} else {
return false;
}


}


}
?>
